package com.liyuan.yiqixing.controller;

import com.liyuan.yiqixing.dto.SimpleSignupAnswer;
import com.liyuan.yiqixing.dto.SimpleSignupInfo;
import com.liyuan.yiqixing.model.Item;
import com.liyuan.yiqixing.model.ResultJson;
import com.liyuan.yiqixing.model.Signup;
import com.liyuan.yiqixing.model.User;
import com.liyuan.yiqixing.service.AuthenticationService;
import com.liyuan.yiqixing.service.ItemService;
import com.liyuan.yiqixing.service.SignupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SignupInfoController {
    @Autowired
    SignupService signupService;
    @Autowired
    ItemService itemService;
    @Autowired
    AuthenticationService authenticationService;
    @PostMapping("/signupInfo/retrieveSignupInfoAsHostel")
    public ResultJson<Object> retrieveSignupInfoAsHostel(){
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Signup[] signups = signupService.retrieveSignupInfosByHostelId(principal.getId());
        SimpleSignupAnswer[] simpleSignupAnswers =transferSignupsToSimpleSignupAnwers(signups);
        return new ResultJson<>(200,"retrieve signupInfo as hostel worker success!",simpleSignupAnswers);
    }

    private SimpleSignupAnswer[] transferSignupsToSimpleSignupAnwers(Signup[] signups) {
        SimpleSignupAnswer[]simpleSignupAnswers = new SimpleSignupAnswer[signups.length];
        for(int i=0;i<simpleSignupAnswers.length;i++){
            User user = authenticationService.retrievePartOfUser(signups[i].getUserId());
            simpleSignupAnswers[i] = new SimpleSignupAnswer();
            simpleSignupAnswers[i].setSignupId(signups[i].getId());
            simpleSignupAnswers[i].setAccount(user.getAccount());
            simpleSignupAnswers[i].setUserId(user.getId());
            simpleSignupAnswers[i].setNickname(user.getNickname());
            simpleSignupAnswers[i].setStatus(signups[i].getStatus());
        }
        return simpleSignupAnswers;
    }

    @PostMapping("/signupInfo/retrieveSignupInfoAsVolunteerWorker")
    public ResultJson<Object> retrieveSignupInfoAsVolunteerWorker(){
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Signup[] signups = signupService.retrieveSignupInfos(principal.getId());
        SimpleSignupInfo[] simpleSignupInfos = transferSignupsToSimpleSignupInfos(signups);
        return new ResultJson<>(200,"retrieve signupInfo as volunteer worker success!",simpleSignupInfos);
    }

    private SimpleSignupInfo[] transferSignupsToSimpleSignupInfos(Signup[] signups) {
        SimpleSignupInfo[] simpleSignupInfos = new SimpleSignupInfo[signups.length];
        for(int i=0;i<signups.length;i++){
            Item item = itemService.retrievePartOfItem(signups[i].getItemId());
            simpleSignupInfos[i] = new SimpleSignupInfo();
            simpleSignupInfos[i].setAvatarUrl(item.getUser().getAvatarUrl());
            simpleSignupInfos[i].setEndTime(item.getEndTime());
            simpleSignupInfos[i].setStartTime(item.getStartTime());
            simpleSignupInfos[i].setIntroduction(item.getIntroduction());
            simpleSignupInfos[i].setItemId(item.getId());
            simpleSignupInfos[i].setStatus(signups[i].getStatus());
            simpleSignupInfos[i].setHostelName(item.getUser().getNickname());
        }
        return simpleSignupInfos;
    }

    private Item[] transferSignupsToItems(Signup[] signups) {
        Item[] items = new Item[signups.length];
        for(int i=0;i<signups.length;i++){
            items[i] = itemService.retrievePartOfItem(signups[i].getItemId());
        }
        return items;
    }
    @PostMapping("/signupInfo/notAccept/{signupId}")
    public ResultJson<Object> notAccept(@PathVariable("signupId")Integer signupId){
        signupService.updateSignupStatusToNotAccept(signupId);
        return new ResultJson<>(200,"修改成功；1->0",null);
    }

    @PostMapping("/signupInfo/accept/{signupId}")
    public ResultJson<Object> accept(@PathVariable("signupId")Integer signupId){
        signupService.updateSignupStatusToAccept(signupId);
        return new ResultJson<>(200,"修改成功；0->1",null);
    }
}
